# Copyright (C) Kumo inc. and its affiliates.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

# To test functions being added by dynamically linked libraries, we compile
# `DynamicFunction.cpp` as a small .so library, and use the
# POLLUX_TEST_DYNAMIC_LIBRARY_PATH macro to locate the .so binary.


set(COMMON_LIBRARY_LINK_OPTIONS)
if(APPLE)
  list(APPEND COMMON_LIBRARY_LINK_OPTIONS "-Wl -undefined,dynamic_lookup")
else()
  # This ensures compatibility during Linux compilation by preventing errors
  # related to 'is being linked both statically and dynamically into this
  # executable,' particularly for melon_hazptr_use_executor."
  list(APPEND COMMON_LIBRARY_LINK_OPTIONS "-Wl,--exclude-libs,ALL" "-Wl,-E")
endif()



kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_function_dynamic
        SOURCES
        dynamic_function.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        LINKOPTS
        ${COMMON_LIBRARY_LINK_OPTIONS}
        LINKS
        pollux::pollux
        pollux::pollux_testing_static
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)
target_link_options(pollux_function_dynamic_shared PRIVATE
        ${COMMON_LIBRARY_LINK_OPTIONS})
kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_overwrite_int_function_dynamic
        SOURCES
        dynamicInt_function_overwrite.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        pollux::pollux
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_overwrite_varchar_function_dynamic
        SOURCES
        dynamic_varchar_function_overwrite.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        pollux::pollux
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_function_err_dynamic
        SOURCES
        dynamic_err_function.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        pollux::pollux
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_overload_int_function_dynamic
        SOURCES
        dynamicInt_function_overload.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        pollux::pollux
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME pollux_overload_varchar_function_dynamic
        SOURCES
        dynamic_varchar_function_overload.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        pollux::pollux
        GTest::gtest
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_test(
        NAME dynamic_link_test
        MODULE function
        SOURCES
        dynamic_link_test.cpp
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        -fno-access-control
        DEFINES
        POLLUX_TEST_DYNAMIC_LIBRARY_PATH="${CMAKE_CURRENT_BINARY_DIR}"
        POLLUX_TEST_DYNAMIC_LIBRARY_PATH_SUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}"
        LINKS
        pollux::pollux
        pollux::pollux_testing_static
        GTest::gtest
        GTest::gmock
        GTest::gtest_main
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_test(
        NAME dynamic_link_main
        MODULE function
        SOURCES
        main.cc
        CXXOPTS
        -fno-access-control
        ${KMCMAKE_CXX_OPTIONS}
        DEFINES
        POLLUX_TEST_DYNAMIC_LIBRARY_PATH="${CMAKE_CURRENT_BINARY_DIR}"
        POLLUX_TEST_DYNAMIC_LIBRARY_PATH_SUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}"
        LINKOPTS
        ${COMMON_LIBRARY_LINK_OPTIONS}
        LINKS
        "-Wl,-E"
        pollux::pollux
        pollux::pollux_testing_static
        GTest::gtest
        GTest::gmock
        GTest::gtest_main
        ${KMCMAKE_DEPS_LINK}
)
